package com.zzgele.qmzb.api.web;

import com.alibaba.boot.dubbo.annotation.DubboConsumer;
import com.github.pagehelper.PageInfo;
import com.zzgele.qmzb.api.core.Result;
import com.zzgele.qmzb.api.core.ResultGenerator;
import com.zzgele.qmzb.api.model.Fans;
import com.zzgele.qmzb.api.model.User;
import com.zzgele.qmzb.api.service.FansService;
import com.zzgele.qmzb.api.service.RoomService;
import com.zzgele.qmzb.api.service.UserService;
import com.zzgele.qmzb.api.utils.DateUtil;
import com.zzgele.qmzb.api.utils.PageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by Martin on 2017/07/13.
*/
@RestController
@RequestMapping("/fans")
public class FansController {
    private final Logger logger = LoggerFactory.getLogger(FansController.class);
    @DubboConsumer
    private FansService fansService;
    @DubboConsumer
    private UserService userService;
    @DubboConsumer
    private RoomService roomService;
    @PostMapping("/create")
    public Result create(Long anchor_uid, Long login_uid) {
        if(anchor_uid.longValue() == login_uid.longValue()) {
            return ResultGenerator.genFailResult("不能关注自己");
        }
        logger.debug("关注 anchor_uid {},login_uid {}",anchor_uid,login_uid);
        User userRefer = userService.findBy("id", anchor_uid);
        if (userRefer == null) {
            return ResultGenerator.genFailResult("对象不存在或已禁用");
        }


        List<Fans> fansList = fansService.findByConditions(anchor_uid,login_uid);
        if (fansList.size() != 0) {
            return ResultGenerator.genSuccessResult("重复添加成功！");
        }

        Fans fans = new Fans();
        fans.setUid(anchor_uid);
        fans.setFansUid(login_uid);
        fansService.save(fans);

        return ResultGenerator.genSuccessResult("添加成功");
    }

    @PostMapping("/fans_list") //我的粉丝=关注我的=(UID=我)
    public Result fans_list(Long login_uid, Integer page, Integer page_size) {
        PageUtil.startPage(page, page_size);
        Map<String, Object> map = new HashMap();
        map.put("uid", login_uid);
        List<HashMap> fansList = fansService.getList(map);
        HashMap fansListHashMap = PageUtil.getPageInfoHashMap(new PageInfo(fansList));

        return ResultGenerator.genSuccessResult(fansListHashMap);
    }

    @PostMapping("/follow_list") //我的关注=我关注的=(FANS_UID=我)
    public Result follow_list(Long login_uid, Integer page, Integer page_size) {
        PageUtil.startPage(page, page_size);
        Map<String, Object> map = new HashMap();
        map.put("fans_uid", login_uid);
        List<HashMap> followList = fansService.getList(map);
        HashMap followListHashMap = PageUtil.getPageInfoHashMap(new PageInfo(followList));

        return ResultGenerator.genSuccessResult(followListHashMap);
    }


    @PostMapping("/index_follow_list") //首页-关注(UID=我)
    public Result index_follow_list(Long login_uid, Integer page, Integer page_size) {
        PageUtil.startPage(page, page_size);
        Map<String, Object> map = new HashMap();
        map.put("uid", login_uid);
        List<HashMap> indexFollowList = fansService.getIndexFollowList(map);
        HashMap indexFollowListHashMap = PageUtil.getPageInfoHashMap(new PageInfo(indexFollowList));
        int  living_count=roomService.getLivingCount();
        Map<String, Object> result = new HashMap();
        result.put("living_count", living_count);
        result.put("follow_list", indexFollowListHashMap);
        return ResultGenerator.genSuccessResult(result);
    }


    @PostMapping("/contribution_rank_list") //直播间-粉丝贡献榜
    public Result contribution_rank_list(Long anchor_uid, Integer page, Integer page_size) {
        PageUtil.startPage(page, page_size);
        Map<String, Object> map = new HashMap();
        map.put("tuid", anchor_uid);
        List<HashMap> contributionRankList = fansService.getContributionRankList(map);
        HashMap contributionRankListHashMap = PageUtil.getPageInfoHashMap(new PageInfo(contributionRankList));

        return ResultGenerator.genSuccessResult(contributionRankListHashMap);
    }
    @PostMapping("/room/contribution/list") //直播间-粉丝贡献榜   当前直播间
    public Result contribution_room_list(Long anchor_uid, Integer page, Integer page_size) {
        PageUtil.startPage(page, page_size);
        Map<String, Object> map = new HashMap();
        int a= DateUtil.getCurUnixTimeStampInt().intValue()-3600*24;
        map.put("time", a);
        map.put("tuid", anchor_uid);
        List<HashMap> contributionRankList = fansService.contributionRoomList(map);
        HashMap contributionRankListHashMap = PageUtil.getPageInfoHashMap(new PageInfo(contributionRankList));

        return ResultGenerator.genSuccessResult(contributionRankListHashMap);
    }

    @PostMapping("/fans_rank_list") //榜单-粉丝榜  粉丝数
    public Result fans_rank_list(Integer page, Integer page_size) {
        PageUtil.startPage(page, page_size);
        List<HashMap> fansRankList = fansService.getFansRankList();
        HashMap fansRankListHashMap = PageUtil.getPageInfoHashMap(new PageInfo(fansRankList));
        return ResultGenerator.genSuccessResult(fansRankListHashMap);
    }


    @PostMapping("/delete")
    public Result delete(Long anchor_uid, Long login_uid) {


        int count = fansService.deleteByConditions(anchor_uid,login_uid);

        return ResultGenerator.genSuccessResult("删除成功");
    }

}
